var app = getApp() 
var pageObject = {
  data: {
    product:{id:'', nums:'1'}, 
    userInfo:{address:'',phone:''}, 
    orderId:'', 
    modalHidden:true
  },
  onLoad: function(options) { 
    this.setData({
      'product.nums': options.nums||1,
      'product.id': options.productId||'', 
      'orderId': options.orderId||'',  
    });
    var loginUserId = wx.getStorageSync('LOGINUSER_ID') || '';  
    var _this = this;
      wx.request({ 
        url: app.globalData.user_detail_url,
        data: {userId:loginUserId},
        method: 'POST', 
        success: function (res) {
          var resultData = res.data;
          if(resultData.errno == 0){ 
            _this.setData({userInfo: resultData.data})   
            if(_this.data.userInfo.phone == '' || _this.data.userInfo.address==''){
              wx.redirectTo({
                url: '../info-center/info-center'
              })
            }else{ 
              _this.getProductInfo(_this.data.product.id)
            }
          }
        },
        fail: function () {
          setTimeout(function () {
            wx.showToast({
              title: "加载失败"
            })
          }, 100)
        }
      })
  },
  getProductInfo: function(id) {  
    var _this = this; 
    wx.request({ 
      url: app.globalData.product_detail_url,
      data: {
        id : id
      },
      method: 'GET', 
      success: function (res) {
        var resultData = res.data;
        if(resultData.errno==0){           
          resultData.data['nums'] = _this.data.product.nums; 
          resultData.data['total'] = _this.data.product.nums * resultData.data.price;
          _this.setData({ 
            product:resultData.data,
          })
        }
      },
      fail: function () {
        setTimeout(function () {
          wx.showToast({
            title: "加载失败"
          })
        }, 100)
      }
    }) 
  }, 
  inputNums: function(e){
    var _this =this;
    this.setData({
      'product.nums': e.detail.value, 
      'product.total': e.detail.value * _this.data.product.price,
    })
  },
  generateOrder: function() {
    app.showLoading('加载中...');  
    var loginUserId = wx.getStorageSync('LOGINUSER_ID') || '';
    var _this = this;
    if (_this.data.orderId == ''){   
      wx.request({ 
        url: app.globalData.buy_buy_url,
        data: {userId:loginUserId,productId:_this.data.product.id,name:_this.data.product.name,nums:_this.data.product.nums,finish:0,price:_this.data.product.price,name:_this.data.product.name,factory:_this.data.product.factory},
        method: 'POST', 
        success: function (res) {
          var resultData = res.data;
          if(resultData.errno == 0){
            _this.setData({
              modalHidden: false
            }) 
            /* app.showModel("购买结果",resultData.message); 
            setTimeout(function () {
              wx.switchTab({
                url: '../product-list/product-list'
              })
            }, 1000)
            */
          }
        },
        fail: function () {
          setTimeout(function () {
            wx.showToast({
              title: "加载失败"
            })
          }, 100)
        }
      })   
    }else{ 
      _this.setData({
        modalHidden: false
      }) 
      /*
      wx.request({ 
        url: app.globalData.buy_commit_url,
        data: {id:_this.data.orderId,finish:1,nums:_this.data.product.nums,total:_this.data.product.nums * _this.data.product.price},
        method: 'POST', 
        success: function (res) {
          var resultData = res.data;
          if(resultData.errno == 0){ 
            app.showModel("购买结果",resultData.message); 
            setTimeout(function () {
              wx.switchTab({
                url: '../product-list/product-list'
              })
            }, 1000)
          }
        } 
      }) 
      */
  }

  },
  modalCandel: function() { 
    this.setData({
      modalHidden: true
    })
  }, 
  modalConfirm: function() { 
    this.setData({
      modalHidden: true
    })
  }
}

Page(pageObject)
